﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;
using XCI.Windows.Helper;

namespace XCI.Windows
{
    public class LoadMask:IDisposable
    {
        private readonly System.Timers.Timer _time;
        private IOverlaySplashScreenHandle handle = null;
        private Control _owner = null;
        private OverlayWindowOptions _options = null;
        private bool _stop;

        public LoadMask()
        {
            _time = new System.Timers.Timer();
            _time.AutoReset = false;
            _time.Enabled = false;
            _time.Interval = 1000;
            _time.Elapsed += _time_Elapsed;
        }

        public void Show(Control owner, int interval = 300)
        {
            Show(owner, null, interval);
        }

        public void Show(Control owner, OverlayWindowOptions options, int interval)
        {
            _owner = owner;
            _options = options;
            _stop = false;
            _time.SynchronizingObject = owner;
            _time.Interval = interval;
            _time.Start();
        }

        public void Hide()
        {
            _stop = true;
            _time.Stop();
            SplashHelper.CloseProgressPanel(handle);
        }

        private void _time_Elapsed(object sender, EventArgs e)
        {
            if (_stop == false)
            {
                handle = SplashHelper.ShowProgressPanel(_owner, _options);
            }
            else
            {
                SplashHelper.CloseProgressPanel(handle);
            }
        }

        public void Dispose()
        {
            _time.Dispose();
        }
    }
}